package Client;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.Graphics;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;

//The main panel in the game. Implements a listener that listens for each timer tick and calls the actionPerformed each tick.
public class GamePanel extends JPanel implements ActionListener, KeyListener, Runnable{
	
	//Instance Variables
	protected DataInputStream i;
	protected DataOutputStream o;
	protected TextArea output;
	protected TextField input;
	protected Thread listener;
	private Timer timer;
	
	//The gamePanel constructor
	public GamePanel(int w, int h, InputStream i, OutputStream o)
	{
		this.setPreferredSize(new Dimension(w, h));
		this.setDoubleBuffered(true);
		this.i = new DataInputStream (new BufferedInputStream (i));
	    this.o = new DataOutputStream (new BufferedOutputStream (o));
		this.timer = new Timer(33, this);setLayout (new BorderLayout ());
	   
		setLayout(null);
		
		output = new TextArea();
		input = new TextField();
		output.setBounds(10, 420, 400, 150);
		input.setBounds(10, 570, 400, 20);
		add(output);
		add(input);
		
	    output.setEditable (false);
	    input.addKeyListener(this);
	    this.setVisible(true);
	    input.requestFocus ();
	    listener = new Thread (this);
	    listener.start ();
	}
	
	//This paints to the GamePanel. This is where we will put our logic for showing the characters in the area and other stuff.
	public void paintComponent(Graphics g) 
    {
		
    }

	//This is what runs on each timer tick. (Which right now is about 30 frames per second)
	@Override
	public void actionPerformed(ActionEvent arg0) {
		//Update the screen by repainting (calling the paintComponent method.
		if(arg0.getSource().equals(timer))
			this.update(this.getGraphics());
	}

	public void killConnection()
	{
		try
		{
			this.i.close();
			this.o.flush();
			this.o.close();
		}
		catch (IOException e)
		{
			output.append("Couldn't disconnect safely.");
		}
	}
	
	@Override
	public void run() {
		 try {
		      while (true) {
		        String line = i.readUTF ();
		        output.appendText (line + "\n");
		      }
		    } catch (IOException ex) {
		      ex.printStackTrace ();
		    } finally {
		      listener = null;
		      input.hide ();
		      validate ();
		      try {
		        o.close ();
		      } catch (IOException ex) {
		        ex.printStackTrace ();
		      }
		    }
	}

	@Override
	public void keyPressed(KeyEvent e) {
		if(e.getKeyCode() == (KeyEvent.VK_ENTER) && input.isFocusOwner())
		{
		    try
		      {
		        o.writeUTF ("/a" + input.getText());
		        o.flush();
		      } 
		      catch (IOException ex) 
		      {
		        ex.printStackTrace();
		        listener.stop ();
		      }
		      input.setText (""); 
		}
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
}
